<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Event/classes/PhpFox_ComponentAdd.class.php, $Author:  $ */



/** Component for adding new events
 * @version $Id: PhpFox_ComponentAdd.class.php 130 2007-12-24 10:30:03Z phpfoxsv $
 * @package module.event
 *
 */
class PhpFox_Mod_Event_ComponentAdd extends BaseComponent
{
    function process()
    {
    	App::membersOnly();

        $oEventItem = $this->_oModule->getItem('Event');
        /* @var $oEventItem PhpFox_Mod_Event_ItemEvent */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = $this->_oModule->getTemplate();
        $oReq = &App::getRequest();
        App::loadClass('Validator');
        App::loadClass('Text');
        $oTxt = &new Text();

        $oSecurityService->checkPerm('events', 'full');
        $oSecurityService->checkAccess('events');
        $sUser = $oSecurityService->getCurrentUserLogin();

        $aCategories = array();
        foreach(App::getTxtFile('event_items.txt') as $sCategory)
        {
            $aCategories[$sCategory] = $sCategory;
        }       
        $aCountries = array();
        foreach (App::getTxtFile('country.txt') as $sCountry)
        {
            $aCountries[$sCountry] = $sCountry;
        }
        $aStates = array();
        foreach (App::getTxtFile('us_stat.txt') as $sState)
        {
            $aStates[$sState] = $sState;
        }
        $aDays = App::getOptions('day');
        $aMonths = App::getOptions('month');
        $aYears = array();
        $iYear = date('Y');
        for ($i = $iYear; $i <= ($iYear + 20); $i++)
        {
            $aYears[$i] = $i;
        }
        $aHours = array();
        for ($i = 1; $i <= 12; $i++)
        {
            $sHour = $i;
            if (strlen($sHour) == 1)
            {
                $sHour = '0'.$sHour;
            }
            $aHours[$sHour] = $sHour;
        }
        $aMinutes = array();
        for ($i = 0; $i <= 55; $i += 5)
        {
            $sMinute = $i;
            if (strlen($sMinute) == 1)
            {
                $sMinute = '0'.$sMinute;
            }
            $aMinutes[$sMinute] = $sMinute;
        }
        $aFields = array
        (
        	'val[title]'    => array('title' => App::format('Mod_Event.form_title'), 'def' => 'required'),
        	'val[email]'    => array('title' => App::format('Mod_Event.form_email'), 'def' => 'email'),
        	'val[type]'     => array('title' => App::format('Mod_Event.form_category'), 'def' => 'required'),
        	'val[short]'    => array('title' => App::format('Mod_Event.form_short_descr'), 'def' => 'required'),
        	'val[city]'     => array('title' => App::format('Mod_Event.form_city'), 'def' => 'required'),
        	'val[country]'  => array('title' => App::format('Mod_Event.form_country'), 'def' => 'required'),
        );
        if (App::getSetting('us_states'))
        {
            // $aFields['val[zip]']   = array('title' => App::format('Mod_Event.form_zip'), 'def' => 'required');
            // $aFields['val[state]'] = array('title' => App::format('Mod_Event.form_state'), 'def' => 'required');
        }
        else
        {
            $aValue['state'] = '';
            $aValue['zip']   = '';
            $aStates[] = '';
        }

        $oValidator = &new Validator($aFields, array());

        if ($oReq->getAction() == 'add')
        {
            $aValue = $oReq->getArray('val');
            if ($aValue['country'] != App::format('Mod_Language.united_states'))
            {
                $aValue['state'] = '';
                $aValue['zip']   = '';
                $aStates[] = '';
                if (App::getParam('us_states'))
                {
                    unset($oValidator->aFields['val[zip]']);
                    unset($oValidator->aFields['val[state]']);
                }
            }
            else 
            {
                if (App::getParam('us_states'))
                {            	
                	// $oValidator->aFields['val[zip]']   = array('title' => App::format('Mod_Event.form_zip'), 'def' => 'required');
            		// $oValidator->aFields['val[state]'] = array('title' => App::format('Mod_Event.form_state'), 'def' => 'required');
                }
            }          
     
            if (!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }            
           
            if (!$aValue['text'])
            {
                $this->addErrors(App::format('Mod_Event.error.text'));
            }
            if (!count($this->getErrors()))
            {
                $sStartTime = $aValue['start_time_hour'].':'.$aValue['start_time_minute'].' '.$aValue['start_time_ap'];
                $oEventItem->aData['user']       = $sUser;
                $oEventItem->aData['type']       = $aValue['type'];
                $oEventItem->aData['title']      = $oTxt->preParseText($aValue['title']);
                $oEventItem->aData['email']      = $aValue['email'];
                //$oEventItem->aData['private']    = 1;
                $oEventItem->aData['private']    = ($aValue['private'] == 1) ? 1 : 2;
                $oEventItem->aData['short']      = $oTxt->preParseText($aValue['short']);
                $oEventItem->aData['text']       = $oTxt->preParseText($aValue['text']);
                $oEventItem->aData['month']      = $aValue['month'];
                $oEventItem->aData['day']        = $aValue['day'];
                $oEventItem->aData['year']       = $aValue['year'];
                $oEventItem->aData['start_time'] = $sStartTime;
                $oEventItem->aData['place']      = $oTxt->preParseText($aValue['place']);
                $oEventItem->aData['address']    = $oTxt->preParseText($aValue['address']);
                $oEventItem->aData['city']       = $oTxt->preParseText($aValue['city']);
                $oEventItem->aData['state']      = $oTxt->preParseText($aValue['state']);
                $oEventItem->aData['zip']        = $aValue['zip'];
                $oEventItem->aData['country']    = $aValue['country'];
                $oEventItem->aData['time']       = time();
                $iEventId = $oEventItem->insert();
                App::gotoUrl('public.event.view', array('id' => $iEventId));
            }
        }
        else
        {
            $oCurrUser = $oSecurityService->getCurrentUser();
            $aValue['email']   = $oCurrUser->aData['email'];
            $aValue['day']     = date('d');
            $aValue['month']   = date('m');
            $aValue['year']    = date('Y');
            $aValue['private'] = 1;
        }
        $oTpl->assignSrc('sJsVal', $oValidator->makeJS());
        $oTpl->assignSrc('sJsMess', $oValidator->makeJsMess());
        $oTpl->assign(array(
        	'aCategories' => $aCategories,
        	'aCountries'  => $aCountries,
            'aErrors'     => $this->getErrors(),
        	'aHours'      => $aHours,
        	'aMinutes'    => $aMinutes,
            'bZipState'   => App::getSetting('us_states'),
        	'aStates'     => $aStates,
        	'aDays'       => $aDays,
        	'aMonths'     => $aMonths,
        	'aYears'      => $aYears,
        	'aValue'      => $aValue,
        ));
        return $oTpl->fetch('AddForm.html');
    }
}
?>